home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / ti803.zip / TI803.TXT
Text File  |  1992-08-12  |  5KB  |  62 lines

  1.  
  2. PRODUCT  :  C++                                    NUMBER  :  803             
  3. VERSION  :  All                                                               
  4. OS  :  PC DOS                                                                 
  5. DATE  :  February 25, 1992                        PAGE  :  1/1                
  6.                                                                               
  7. TITLE  :  Dynamically Allocated Multidimensional Arrays                       
  8.                                                                               
  9.                                                                               
  10.                                                                               
  11.                                                                               
  12. #include <stdio.h>                                                            
  13. #include <alloc.h>                                                            
  14.                                                                               
  15. #define     ROW         10                                                    
  16. #define     COLUMN      15                                                    
  17.                                                                               
  18. typedef int ARRAYTYPE;                                                        
  19. typedef ARRAYTYPE * ARRAYTYPEPTR;                                             
  20.                                                                               
  21. /*  Declare the 2D array as a pointer to a pointer to                         
  22. ARRAYTYPE  */                                                                 
  23. /*  Note there is no memory allocated for the array yet.                      
  24. */                                                                            
  25.                                                                               
  26. ARRAYTYPEPTR *array;                                                          
  27.                                                                               
  28. void main()                                                                   
  29. {                                                                             
  30. int i, j;                                                                     
  31.                                                                               
  32. /*  First, allocate an array of pointers to ARRAYTYPE of ROW                  
  33. */                                                                            
  34. /*  elements.  Next, looping through the array of pointers,                   
  35. */                                                                            
  36. /*  allocate arrays of ARRAYTYPE of COLUMN elements and                       
  37. assign   */                                                                   
  38. /*  them to the pointers.  This assigns the necessary memory                  
  39. to  */                                                                        
  40. /*  the array, which you can then index via the [] operators.                 
  41. */                                                                            
  42.                                                                               
  43. array = (ARRAYTYPEPTR *) malloc (ROW * sizeof(ARRAYTYPEPTR));                 
  44. for (i = 0; i < ROW; i++)                                                     
  45. array[i] = (ARRAYTYPEPTR) malloc (COLUMN * sizeof                             
  46. (ARRAYTYPE));                                                                 
  47.                                                                               
  48. /*  Fill array with unique values  */                                         
  49.                                                                               
  50. for (i = 0; i < ROW; i++)                                                     
  51. for (j = 0; j < COLUMN; j++)                                                  
  52. array[i][j] = (COLUMN * i) + j;                                               
  53.                                                                               
  54. /*  Print out array to make sure everything's OK  */                          
  55. for (i = 0; i < ROW; i++)    {                                                
  56. for (j = 0; j < COLUMN; j++)                                                  
  57. printf("%4d",array[i][j]);                                                    
  58. putchar('\n');                                                                
  59. }                                                                             
  60. }                                                                             
  61.                                                                               
  62.